﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using SpaceShooter.Settings;

namespace SpaceShooter.SceneCamera
{
    public static class Camera
    {
        /// <summary>
        /// the view Matrix of this camera
        /// </summary>
        private static Matrix mViewMatrix;

        /// <summary>
        /// the view Matrix of this camera
        /// </summary>
        public static Matrix ViewMatrix
        {
            get { return Camera.mViewMatrix; }
            set { Camera.mViewMatrix = value; }
        }

        /// <summary>
        /// the projection Matrix of this camera
        /// </summary>
        private static Matrix mProjectionMatrix;

        /// <summary>
        /// the projection Matrix of this camera
        /// </summary>
        public static Matrix ProjectionMatrix
        {
            get { return Camera.mProjectionMatrix; }
            set { Camera.mProjectionMatrix = value; }
        }

        /// <summary>
        /// the position of the camera
        /// </summary>
        private static Vector3 mPosition;

        /// <summary>
        /// the position of this camera
        /// </summary>
        public static Vector3 Position
        {
            get { return Camera.mPosition; }
            set { Camera.mPosition = value; }
        }

        /// <summary>
        /// the target of this camera
        /// </summary>
        private static Vector3 mTarget;

        /// <summary>
        /// the target of this camera
        /// </summary>
        public static Vector3 Target
        {
            get { return Camera.mTarget; }
            set { Camera.mTarget = value; }
        }

        /// <summary>
        /// the up vector of the camera
        /// </summary>
        private static Vector3 mUp;

        /// <summary>
        /// the up vetor of the camera
        /// </summary>
        public static Vector3 Up
        {
            get { return Camera.mUp; }
            set { Camera.mUp = value; }
        }

        /// <summary>
        /// Create View Matrix
        /// </summary>
        /// <param name="pPosition">the position of the camera</param>
        /// <param name="pTarget">the target of the camera</param>
        /// <param name="pUp">the up vector of the camera</param>
        public static void CreateViewMatrix(Vector3 pPosition, Vector3 pTarget, Vector3 pUp)
        {
            mPosition = pPosition;
            mTarget = pTarget;
            mUp = pUp;
            mViewMatrix = Matrix.CreateLookAt(mPosition, mTarget, mUp);
        }

        /// <summary>
        /// Create View Matrix
        /// </summary>
        /// <param name="pPosition">the position of the camera</param>
        public static void CreateViewMatrix(Vector3 pPosition)
        {
            mPosition = pPosition;
            mViewMatrix = Matrix.CreateLookAt(mPosition, mTarget, mUp);
        }

        /// <summary>
        /// Create View Matrix
        /// </summary>
        /// <param name="pPosition">the position of the camera</param>
        /// <param name="pTarget">the target of the camera</param>
        public static void CreateViewMatrix(Vector3 pPosition, Vector3 pTarget)
        {
            mPosition = pPosition;
            mTarget = pTarget;
            mViewMatrix = Matrix.CreateLookAt(mPosition, mTarget, mUp);
        }

        /// <summary>
        /// Load all the paramters of the camera should needed
        /// </summary>
        public static void LoadParameters()
        {
            mPosition = GameSettings.CameraSettings.InitPosition;
            mTarget = GameSettings.CameraSettings.InitTarget;
            mUp = GameSettings.CameraSettings.InitUpVector;
            mViewMatrix = Matrix.CreateLookAt(mPosition, mTarget, mUp);
            mProjectionMatrix = Matrix.CreatePerspectiveFieldOfView
                (GameSettings.CameraSettings.FieldOfView, 
                GameSettings.SystemSettings.AspectRatio, 
                GameSettings.CameraSettings.NearPlane, 
                GameSettings.CameraSettings.FarPlane);
        }

        /// <summary>
        /// Create the projection matrix of the camera
        /// </summary>
        public static void CreateProejctionMatrix()
        {
            mProjectionMatrix = Matrix.CreatePerspectiveFieldOfView
                (GameSettings.CameraSettings.FieldOfView,
                GameSettings.SystemSettings.AspectRatio,
                GameSettings.CameraSettings.NearPlane,
                GameSettings.CameraSettings.FarPlane);
        }
    }
}
